/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 14, 2012  12:11:18 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : PaymentRequestOutgoingServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.promis.construction.PaymentRequestInstallment;
import jpa.entitys.promis.construction.PaymentRequestOutgoing;
import jpa.entitys.promis.construction.PaymentRequestOutgoingPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class PaymentRequestOutgoingServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(PaymentRequestOutgoingServices.class);
	
	/**
	 * @param em
	 */
	public PaymentRequestOutgoingServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	

	public PaymentRequestOutgoing findById(PaymentRequestOutgoingPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "PaymentRequestOutgoing.findById"));				
	}
	
	public List<PaymentRequestOutgoing> findByPB(String company, String project, long requestNo, String requestType, String requestSeries, long seq, long seqNo, String syncExternalRefNo, String status) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);			
		mapWhereValue.put("requestType", requestType);
		mapWhereValue.put("requestNo", requestNo);
		mapWhereValue.put("requestSeries", requestSeries);
		mapWhereValue.put("seq", seq);
		mapWhereValue.put("seqNo", seqNo);
		mapWhereValue.put("syncExternalRefNo", syncExternalRefNo);
		mapWhereValue.put("status", status);
				
		return super.ConvertToYoureObject(super.find("PaymentRequestOutgoing.findByPB", mapWhereValue));		
	}
	
	public PaymentRequestOutgoing InsertedOrUpdated(PaymentRequestOutgoing vlaue) throws Exception {
		PaymentRequestOutgoing obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{						
				super.inserted(obj);
			}else{				
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public PaymentRequestOutgoing Inserted(PaymentRequestOutgoing vlaue) throws Exception {
		PaymentRequestOutgoing obj = vlaue;
		try{

			vlaue.setSyncCreatedon(this.getCurrent_date());
			vlaue.setSyncCreatedby(this.getUser());
			
			super.inserted(obj);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<PaymentRequestOutgoing> Inserted(List<PaymentRequestOutgoing> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(PaymentRequestOutgoing obj : vlauelist)
			{
				common_lst.add(obj);
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<PaymentRequestOutgoing> Updated(List<PaymentRequestOutgoing> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(PaymentRequestOutgoing obj : vlauelist)
			{
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				common_lst.add(obj);	
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

	/**
	 * 
	 * @param PaymentRequestOutgoing_lst
	 * @param PaymentRequestInstallment_lst
	 * @param isNew [true = INSERT, false = UPDATE]
	 * @throws Exception
	 */
	public void InsertAndUpdate(List<PaymentRequestOutgoing> PaymentRequestOutgoing_lst, List<PaymentRequestInstallment> PaymentRequestInstallment_lst, boolean isNew)throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> common_lst1 = new ArrayList<CommonEntity>();
			for(PaymentRequestOutgoing obj : PaymentRequestOutgoing_lst)
			{
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				common_lst.add(obj);
					
			}
					
			for(PaymentRequestInstallment obj1 : PaymentRequestInstallment_lst)
			{
				obj1.setSyncUpdatedon(this.getCurrent_date());
				obj1.setSyncUpdatedby(this.getUser());
				common_lst1.add(obj1);
			}
			
			if(CommonService.getSize(common_lst) == CommonService.getSize(PaymentRequestOutgoing_lst) && CommonService.getSize(common_lst1) == CommonService.getSize(PaymentRequestInstallment_lst)){
				InsertUpdated(common_lst,common_lst1, isNew);
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
			System.err.println(e.getMessage());
			log.error(e);
			throw e;
		}
	}
	
	/**
	 * 
	 * @param PaymentRequestOutgoing_lst
	 * @param PaymentRequestInstallment_lst
	 * @param isNew [true = INSERT, false = UPDATE]
	 * @throws Exception
	 */
	private void InsertUpdated(List<CommonEntity> PaymentRequestOutgoing_lst, List<CommonEntity> PaymentRequestInstallment_lst, boolean isNew) throws Exception{
		EntityTransaction t = null;
		try{			
	        t = getEm().getTransaction();
	        t.begin();
	        if(isNew){
	        	for(CommonEntity object : PaymentRequestOutgoing_lst)
		        	getEm().persist(object);		       
	        			
		        		for(CommonEntity object : PaymentRequestInstallment_lst)
		    	        	getEm().merge(object);	
	        }else
	        {
	        	for(CommonEntity object : PaymentRequestOutgoing_lst)
		        	getEm().merge(object);
		        
		        		for(CommonEntity object : PaymentRequestInstallment_lst)
		    	        	getEm().merge(object);	
	        }
	        
	        t.commit();
		        				
		}catch(Exception e)
		{
			t.rollback();
			throw e;
		} finally {
			  if (t.isActive()) 
				  t.rollback();
		}
	}

}
